package com.sxt.sys.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxt.sys.domain.Reply;
import com.sxt.sys.domain.Topic;
import com.sxt.sys.service.ReplyService;
import com.sxt.sys.service.TopicService;
import com.sxt.sys.vo.ReplyVo;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2019-04-29
 */
@RestController
@RequestMapping("/sys/reply")
public class ReplyController {

	@Autowired
	private ReplyService replyService;
	
	@Autowired
	private TopicService topicService;
	
	/**
	 * 根据帖子id查询评论
	 */
	@RequestMapping("loadAllReply")
	public List<Reply> loadAllReply(ReplyVo replyVo){
		QueryWrapper<Reply> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("topicid", replyVo.getTopicid());
		Topic topic = this.topicService.getById(replyVo.getTopicid());
		topic.setClickamount(topic.getClickamount()+1);
		this.topicService.updateById(topic);
		List<Reply> list = this.replyService.list(queryWrapper);
		return list;
	}
	
	/**
	 * 添加评论
	 */
	@RequestMapping("addReply")
	public Map<String, String> addReply(ReplyVo replyVo){
		replyVo.setCreatedate(new Date());
		boolean flag = this.replyService.save(replyVo);
		Map<String, String> map = new HashMap<>();
		if (flag) {
			map.put("msg", "评论成功");
		} else {
			map.put("msg", "评论失败");
		}
		return map;
	}
	
}

